#!/usr/bin/perl
#perl perl_create_illu_tcga.pl  -d ./ILLLUSC -a Human1M-Duov3_B.csv -t ILLLUSC
use Getopt::Std;
#use strict;
#getopt("d:a:e:t", \%args);
my %args;
getopt("a:d:t", \%args);
my $maindir = $args{d};
my $annotation = $args{a};
my $tpedfile =$args{t};
chomp($maindir);
chomp($annotation);
chomp($tpedfile);
open(TPED,">$tpedfile.tped");
open(TFAM,">$tpedfile.tfam");
open BUFF,"<$annotation" or die "can't open $annotation";
my $line = <BUFF>;
my $line;
my %annot;
#storinging Allelle1 & 2 to the hash
while($line = <BUFF>)
{
	chomp($line);
	$line =~ s/"//g;
	my @array = split(',',$line);
	#print $array[3]."\n";
	$array[3] =~ s/\[//g;
	$array[3] =~ s/\]//g;
	@array1 = split(/\//,$array[3]);
	
	#print $array[0]."\t".$array[8]."\t".$array[9]."\n";
	if($array[0] =~ m/-/)
	{
		#print $array[0]."\n";
		@Modify_snpid = split('-',$array[0]);
		pop(@Modify_snpid);
		$array[0] = join('-',@Modify_snpid);
	}
	my $t= $array1[0]."\t".$array1[1];
	if($t =~ m/[AGTC]/)
	{
		$annot{$array[0]} = $t;
		#print $annot{$array[0]}."\n";
	}
	else
	{
		$annot1{$array[0]} = $t;
	}
}

#opening each file in the directory
open(BUFF,"ls $maindir|");
###@maindirarr = split(/\//,$maindir);

my $line;
###$line = pop(@maindirarr);
###$maindir = join('/',@maindirarr);
my $nu = 1;
#$line = <BUFF>;
#asigning the alleles types according to the bird seed output file description
#${"A0"} = 'AA';
#${"A1"} = 'AB';
#${"A2"} = 'BB';
#print $line."\n";
my %mainhash;
my %flaghash;
#$line = <BUFF>;
while($line = <BUFF>)
{
        chomp($line);
#	creating tfam file name
	#@tf = split(/\./,$line);
	#@tfa = split('_6_',$tf[0]);
	#$tfam = "$tfa[1] $tfa[1] 0 0 9 -9";	
	#print $tfam."\n";
	#print TFAM $tfam."\n";
#creating unique file pointer name to each separate level2 genotype file the given directory
	$BUFF = "BUFF$nu";
	open($BUFF,"$maindir/$line");
	my $liney;
	$liney = <$BUFF>;
	chomp($liney);
	#print $liney."\n";
	@arrey = split("\t",$liney);
	for($i=2;$i<@arrey;$i++)
	{
		$arrey[$i] =~ s/\t//g;
		$arrey[$i] =~ s/ //g;
		if($arrey[$i] ne "" )
		{
			print TFAM "$arrey[$i] $arrey[$i] 0 0 9 -9\n";
		}
	}

	$liney = <$BUFF>;
	while($liney=<$BUFF>)
	{
		chomp($liney);
		my @arr =split(/\t/,$liney);
		#print "@arr\n";
		if(exists($annot{$arr[0]}))
		{
			my @anno = split("\t",$annot{$arr[0]});
				$A = $anno[0];
				$B = $anno[1];
			$arr[1] =~ s/XY/25/g;
			$arr[1] =~ s/X/23/g;
			$arr[1] =~ s/Y/24/g;
			$arr[1] =~ s/MT/26/g;
			$arr[1] =~ s/XY/25/g;
			for($jy=3;$jy<@arr;$jy++)
			{	
				if($arr[$jy] eq "AA")
				{
					$genotype = "$A $A";
				}
				elsif($arr[$jy] eq "AB" || $arr[$jy] eq "BA")
				{
					$genotype = "$A $B";
				}
				elsif($arr[$jy] eq "BB")
                        	{
                                	$genotype = "$B $B";
                        	}
				elsif($arr[$jy] eq "NC")
				{
					$genotype = "0 0";
				}
				if($arr[1] ne "25")
				{
					if(exists($mainhash{$arr[0]}))
					{
						$mainhash{$arr[0]} =$mainhash{$arr[0]}." ".$genotype;
					}
					else
					{
						$mainhash{$arr[0]} = $arr[1]." ".$arr[0]." "."0 ".$arr[2]." ".$genotype;
					}
				}
			}
		}
		elsif(exists($annot1{$arr[0]}))
		{
		}
		else
		{
			die "something wrong with this snp id $arr[0]\n";
		}
		
	}
	$nu++;
	close($BUFF);	
}
#print tped file
while(my($key,$value) = each %mainhash)
{
	print TPED $value."\n";
}
